var Each=Array.prototype.forEach;
var baiduKeyMap={
    open_index_flag:false,
    index_prefix:"_bij_",
    items:[],
    index:0,
    indexTo:(i)=>{
        baiduKeyMap.items[baiduKeyMap.index].className=baiduKeyMap.items[baiduKeyMap.index].className.replace(/ active/,'');
        baiduKeyMap.items[baiduKeyMap.index=i].className+=" active";
        let e=baiduKeyMap.items[i],m=baiduKeyMap.items[i].fixed_top,s=document.documentElement.scrollTop,h=document.documentElement.clientHeight;
        if(s>m-e.offsetHeight) {
            baiduKeyMap.jumpTo(s-m-e.offsetHeight);
        }else if(m>s+h){
            let n=m-s-h;
            baiduKeyMap.jumpTo(n,'+');
        }
    },
    jumpTo:(num,type)=>{
        let h=0;
        switch(type) {
            case "+": 
                document.documentElement.scrollTop+=num; break;
            case '-':
                document.documentElement.scrollTop-=num; break;
            default:    
                document.documentElement.scrollTop=num;
        }
    }
};
//找出元素的绝对高度准备定位
Each.call(document.getElementsByClassName('c-container new-pmd'),(e)=>{
    if(e.firstElementChild.tagName!='H3') return;
    let p=e;e.fixed_top=0;
    while(p) {
        e.fixed_top+=p.offsetTop;
        p=p.parentElement;
    }
    baiduKeyMap.items.push(e);
})

document.addEventListener('keydown',function(event){
  if(event.target.tagName=='INPUT') return true;
  let aliasName=keyMap.get(event),name='';

  if(aliasName)
   switch(aliasName ){
       case "f1":
             window.open(chrome.runtime.getURL("readme.html#baidu"));
            return keyMap.stop(event);
       case "esc":          //取消输入焦点 
                document.getElementById('kw').blur();
                document.body.focus();
                break;
       case ":":
       case "f10":        //显示条目前面数字
                Each.call(document.getElementsByTagName('h3'),(e,i)=>{
                    if(baiduKeyMap.open_index_flag) return e.previousElementSibling.remove();
                    let a=document.createElement('a');
                    a.id=baiduKeyMap.index_prefix+i;     a.innerHTML=i+".";   a.className="add_index";
                    e.parentElement.insertBefore(a,e)
                });
                baiduKeyMap.open_index_flag=!baiduKeyMap.open_index_flag;
                return keyMap.stop(event);
                break;
       case "i":            //搜索框获取焦点
           let ee=document.getElementById('kw');
            ee.focus();
            ee.setSelectionRange ?   ee.setSelectionRange(0, ee.value.length): ee.select();
            return keyMap.stop(event);
       case "/":              //搜索模式
       case "v":            //复制模式
                break;
       case 'up':
       case "k":            //上
                baiduKeyMap.indexTo(Math.max(baiduKeyMap.index-1,0));
                break;
       case "down":
       case "j":             //下
                baiduKeyMap.indexTo(Math.min(baiduKeyMap.index+1,baiduKeyMap.items.length-1));
                break;
       case "h":            //左
       case "back": 
           
                break;
       case "l":            //右
            break;
       case "u":   //向上滚半屏    
            baiduKeyMap.jumpTo(document.documentElement.clientHeightt/2,'-');
            break;
       case "f":   //向上滚一屏
            baiduKeyMap.jumpTo(document.documentElement.clientHeight,'-');
            break;
       case "d":   //向下滚半屏
            baiduKeyMap.jumpTo(document.documentElement.clientHeight/2,'+');
            break;
       case "b":   //向下滚一屏幕
            baiduKeyMap.jumpTo(document.documentElement.clientHeight,"+");
            break;
       case "space": 
       case "enter": 
           baiduKeyMap.items[baiduKeyMap.index].firstElementChild.firstElementChild.click();
            break;
       default: 
           if(baiduKeyMap.open_index_flag && aliasName.match(/\d/)){
               if(!keyMap.joinKey.match(/^\d+$/)) keyMap.joinKey=keyMap.joinKey[keyMap.joinKey.length-1];
               if(keyMap.timer0) clearInterval(keyMap.timer0);
               keyMap.timer0=setTimeout(()=>{
                   let e=document.getElementById(baiduKeyMap.index_prefix+keyMap.joinKey)
                   console.log(keyMap.joinKey);
                   if(e) {
                       let input=e.nextElementSibling.children[0];
                       input.click();
                       //select(input);
                   }
                   keyMap.joinKey='';
               },200);
           }
   }
})